package com.autoEcole.service;

import java.util.List;
import java.util.Map;

import com.autoEcole.dao.DocumentDAO;
import com.autoEcole.exception.ApplicationException;
import com.autoEcole.exception.DAOException;
import com.autoEcole.model.Document;

public class DocumentService {
	
	private DocumentDAO documentDAO;
	/**
	 * DocumentService
	 */
	private static DocumentService documentService;
	
	/**
	 * Constructor
	 */
	private DocumentService() {
		super();
	}

	/**
	 * Returns the DocumentService instance (Design Pattern : Singleton)
	 * 
	 * @return DocumentService
	 */
	public static synchronized DocumentService getInstance() {
		if (documentService == null)
			documentService = new DocumentService();
		return documentService;
	}

	public DocumentDAO getDocumentDAO() {
		return documentDAO;
	}

	public void setDocumentDAO(DocumentDAO documentDAO) {
		this.documentDAO = documentDAO;
	}

	public int create(Document document) throws ApplicationException {
		try {
			return documentDAO.create(document);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * 
	 */
	public void update(Document document) throws ApplicationException {
		try {
			documentDAO.update(document);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * retourne l'element si il existe ou null
	 */
	public Document findById(int id) throws ApplicationException {
		try {
			return documentDAO.findById(id);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * 
	 */
	public List<Document> findAll() throws ApplicationException {
		try {
			return documentDAO.findAll();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
	
	/**
	 * 
	 */
	public Map<Integer , Document> findAllMap() throws ApplicationException {
		try {
			return documentDAO.findAllMap();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}
	
	/**
	 * 
	 */	
	public void delete(int documentId) throws ApplicationException {
		try {
			documentDAO.delete(documentId);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}	
}
